package com.yupi.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.project.model.entity.Book;
import com.yupi.project.service.BookService;
import com.yupi.project.mapper.BookMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Dell
* @description 针对表【book(预订记录)】的数据库操作Service实现
* @createDate 2025-07-01 10:00:08
* 注：60 * 1000L是一分钟
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService{

    @Resource
    private BookMapper bookMapper;

    @Override
    public boolean monthlyTimes(Long visitorId){
        long ONE_MONTH = 30 * 24 * 60 * 60 * 1000L;
        Date monthAgo = new Date(new Date().getTime() - ONE_MONTH);
        List<Book> bookInMonth = bookMapper.listBookTimes(monthAgo, visitorId);
        return !bookInMonth.isEmpty();
    }

    @Override
    public int yearlyTimes(Long visitorId){
        long ONE_YEAR = 365 * 24 * 60 * 60 * 1000L;
        Date yearAgo = new Date(new Date().getTime() - ONE_YEAR);
        List<Book> bookInYear = bookMapper.listBookTimes(yearAgo, visitorId);
        return bookInYear.size();
    }

    @Override
    public int getActivity(Long visitorId){
        boolean monthlyTimes = monthlyTimes(visitorId);
        int yearlyTimes = yearlyTimes(visitorId);
        if(monthlyTimes || yearlyTimes >= 6){
            return 0;
        }
        else if(yearlyTimes >= 1){
            return 1;
        }
        else {
            return 2;
        }
    }
}




